//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    titleBarHeight: '',
    tabs: [],
    activeTab: 0,
    list: [],
    lists: [],
    showLoading: true,
    pushOver: false,
    offset: 0,
    alpha: '',
    countryList: [],
    countryLists: [],
    countryIndex: 0,
    countryTabData: [],
    countryTabDatas: [],
    typeId: "",
    type: "",
    wenid: [],
    distilleryid: "", //酒柜传的id
    showView: false, //盒子弹框的显示隐藏
    showColor: false, //筛选的文字颜色变化
    showpin: '', //威士忌头部弹框筛选弹框传参
    showpinItem: false, //威士忌头部弹框筛选弹框传参
    showpinItemOne: false, //威士忌头部弹框筛选弹框传参
    showpinItemTow: false, //威士忌头部弹框筛选弹框传参
    showpinItemThree: false, //威士忌头部弹框筛选弹框传参
    showpinItemFor: false, //威士忌头部弹框筛选弹框传参
    endnumber: '',
    endyaers: '',
    endyaer: '',
    startnumber: '',
    startyaers: '',
    startyaer: '',
    index: 0, //起始页
    des: 50, //结束
    sort_proofbl: false,
    sort_distillation_timebl: false,
    sort_proof: "DESC", //酒精排序赋值
    sort_distillation_time: "DESC", //年份排序赋值
    searChdata: "",
    searChindex: '', //上弹框index
    searChiname: "", //上弹框搜索名字
    searChlist: "",
    searChlists: "",
    target: '',
    seanames: "产地",
    seachtarget: "",
    talk: '',
    isfouce: true,
  },
  onLoad: function (options) {

    if (options.typeId == 'tt') {

      var that = this;
      console.log(options)
      that.setData({
        type: options.typeId,
        typeId: options.typeId
      })
      that.getData()

      that.setData({
        activeTab: 0
      })



      wx.getSystemInfo({
        success: function (res) {
          console.log(res, 'hahahahahhaha')
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })

    } else {
      var that = this;
      console.log(options)
      that.setData({
        typeId: options.typeId
      })
      that.getData(options.typeId)

      that.setData({
        activeTab: options.typeId
      })



      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })
    }


  },
  onTabCLick(e) {
    const index = e.detail.index
    console.log(index)
    //切换栏目，重置数据
    this.setData({
      activeTab: index,
      showLoading: true,
      list: []
    })
    setTimeout((item) => {
      this.getData(index)
    }, 500)
  },
  scrollCountry(e) {
    console.log(e)
  },

  onChange(e) {
    // const index = e.detail.index
    // this.setData({activeTab: index})
  },
  aaa() {},
  navBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  itemMinseach(e) {
    console.log(e)
    if (this.data.showpin == 1) {
      this.setData({
        searChlist: e.currentTarget.dataset.item,
        searChindex: e.currentTarget.dataset.index,
        searChiname: e.currentTarget.dataset.name,
        seanames: e.currentTarget.dataset.name.country,


      })
    } else {
      this.setData({
        searChlists: e.currentTarget.dataset.item,
        searChindex: e.currentTarget.dataset.index,
        searChiname: e.currentTarget.dataset.name,

      })
    }
    console.log(this.data.searChlist)
    this.getData(this.data.typeId)
    this.setData({
      showpinItem: false,
      showpinItemOne: false,
      showpinItemTow: false

    })
  },
  searChlist() {
    this.setData({
      searChlist: "",
      seanames: '产地',
      searChiname: ''
    })
    this.getData(this.data.typeId)
  },
  getsearChdata() {
    let that = this
    app.request({
      url: "/selectBrandcountry",
      data:{
        num:1000,
        offfset:0,
      },
      method:'post',
      success: function (res) {
        console.log(res.data)
        that.setData({
          searChdata: res.data.records
        })
        console.log(that.data.searChdata)
      }
    })
  },
  getData(typeId) {
    console.log(typeId, 111)
    let that = this
    let countryLists = []
    if (that.data.pushOver) return
    let typeArr = ['whisky', 'brand', 'distillery', 'business']
    app.request({
      url: '/' + typeArr[typeId],
      data: {
        'offset': this.data.index,
        "num": this.data.des,
        "country": this.data.searChiname,
        "target": this.data.seachtarget,
        "sort_score": this.data.sort_distillation_time
      },

      method: 'post',
      success: function (res) {
        console.log(res.data.records)
        that.setData({
          list: res.data.records,
        })


      }
    })
  },
  //头部的三个弹框
  showpin(e) {
    this.setData({
      showpin: e.currentTarget.dataset.typeid,

    })
    // showpinItemOne:false,//威士忌头部弹框筛选弹框传参
    // showpinItemTow:false,//威士忌头部弹框筛选弹框传参
    // showpinItemThree:false,//威士忌头部弹框筛选弹框传参
    // showpinItemFor:false,//威士忌头部弹框筛选弹框传参
    console.log(this.data.showpin)

    if (this.data.showpin == 1) {
      this.getsearChdata()
      this.setData({
        showpinItemOne: !this.data.showpinItemOne,
        showpinItemTow: false,
        showpinItemFor: false,
        showpinItemThree: false,
      })
    }

    if (this.data.showpinItemOne || this.data.showpinItemTow || this.data.showpinItemThree || this.data.showpinItemFor == true) {
      this.setData({
        showpinItem: true,
      })
    } else {
      this.setData({
        showpinItem: false,
      })
    }
  },
  //筛选弹框
  searChitem() {
    this.setData({
      showView: !this.data.showView,
      showColor: !this.data.showColor
    })
    console.log(this.data.color)
  },
  searChitems() {
    this.setData({
      showView: !this.data.showView,
      showColor: !this.data.showColor
    })
  },
  talk: function (e) {
    this.setData({
      isfouce: true,
      talk: e.detail.value
    })
  },
  isfouce() {
    this.setData({
      isfouce: false
    })
  },
  //关闭弹框
  clasepin() {
    this.setData({
      showpinItem: false,
      showpinItemOne: false
    })
  },
  getDataseaCh(data, typeId) {
    let that = this
    if (that.data.pushOver) return
    let datas = {
      target: data,
      num: 20,
      offset: that.data.offset
    }
    let typeArr = ['whisky', 'brand', 'distillery', 'business']
    app.request({
      url: '/' + typeArr[typeId] + '/search-list',
      data: datas,
      success: function (res) {
        console.log(res.data.list)

      }
    })
  },
  //评分排序
  particular() {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      index: 0,
      des: 50,
      sort_distillation_timebl: !this.data.sort_distillation_timebl,
    })
    if (this.data.sort_distillation_timebl == true) {
      this.setData({
        index: 0,
        des: 50,
        sort_distillation_time: "ASC",
      })
    } else {
      this.setData({
        index: 0,
        des: 50,
        sort_distillation_time: "DESC",
      })
    }
    this.getData(this.data.typeId)
  },
  goSearch(e) {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      seachtarget: e.detail.value,
      index: 0,
      des: 50
    })
    console.log(e.detail.value)
    this.getData(this.data.typeId)

    // console.log(e)
    // let that = this
    // let data = {
    //   typeId: that.data.activeTab,
    //   data: e.detail.value
    // }
    // console.log(data)
    // wx.navigateTo({
    //   url: '/pages/search/index',
    //   success: function (res) {
    //     // 通过eventChannel向被打开页面传送数据
    //     res.eventChannel.emit('acceptDataFromOpenerPage', data)
    //   }
    // })
  },
  goDes(e) {
    //判断进入页面的路径
    if (this.data.type == 'tt') {
      let index = e.currentTarget.dataset.index;
      let parentindex = e.currentTarget.dataset.parentindex;
      this.data.wenid.push(e.currentTarget.dataset.item.wineid)
      this.data.list[parentindex].datas[index].active = true;
      this.setData({
        list: this.data.list,
        // ['list[' + index + '].datas[' + parentindex + '].active']: true,
        distilleryid: e.currentTarget.dataset.item.wineid,
        wenid: this.data.wenid
      })
      console.log(this.data.wenid)
    } else {
      let data = e.currentTarget.dataset.id
      console.log(data)
      if (this.data.activeTab == 1) {
        // wx.setStorageSync('data', data)
        console.log(JSON.stringify(data))
        console.log(e.currentTarget.dataset.winid, "1111")
        wx.navigateTo({

          url: '/pages/details/index?id=' + this.data.activeTab + '&brandid=' + e.currentTarget.dataset.winid,
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            // res.eventChannel.emit('acceptDataFromOpenerPage', data)
          }
        })
      }
      if (this.data.activeTab == 2) {
        // wx.setStorageSync('data', data)
        console.log(JSON.stringify(data))
        console.log(e, "111122222")
        wx.navigateTo({
          url: '/pages/details/index?id=' + this.data.activeTab + '&brandid=' + e.currentTarget.dataset.id.distilleryid,
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            // res.eventChannel.emit('acceptDataFromOpenerPage', data)
          }
        })
      }
      if (this.data.activeTab == 3) {
        // wx.setStorageSync('data', data)
        console.log(JSON.stringify(data))
        console.log(e, "111122222")
        wx.navigateTo({
          url: '/pages/details/index?id=' + this.data.activeTab + '&brandid=' + e.currentTarget.dataset.id.businessid,
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            // res.eventChannel.emit('acceptDataFromOpenerPage', data)
          }
        })
      } else {
        console.log(e.currentTarget.dataset.item.wineid)

        wx.navigateTo({
          url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
        })
      }
    }



  },
  // goDes(e) {
  //   wx.navigateTo({
  //     url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
  //   })
  //   //判断进入页面的路径
  //   if (this.data.type == 'tt') {
  //     let index = e.currentTarget.dataset.index;
  //     let parentindex = e.currentTarget.dataset.parentindex;
  //     this.data.wenid.push(e.currentTarget.dataset.item.wineid)
  //     this.data.list[parentindex].datas[index].active = true;
  //     this.setData({
  //       list: this.data.list,
  //       // ['list[' + index + '].datas[' + parentindex + '].active']: true,
  //       distilleryid: e.currentTarget.dataset.item.wineid,
  //       wenid: this.data.wenid
  //     })
  //     console.log(this.data.wenid)
  //   } else {
  //     let data = e.currentTarget.dataset.item
  //     console.log(data)
  //     data.typeId = this.data.activeTab
  //     if (data.typeId >= 1) {
  //       // wx.setStorageSync('data', data)
  //       console.log(JSON.stringify(data))
  //       wx.navigateTo({
  //         url: '/pages/details/index?id=' + JSON.stringify(data),
  //         success: function (res) {
  //           // 通过eventChannel向被打开页面传送数据
  //           // res.eventChannel.emit('acceptDataFromOpenerPage', data)
  //         }
  //       })
  //     } else {
  //       console.log(e.currentTarget.dataset.item.wineid)


  //     }
  //   }



  // },
  butt: function () {

    let yy = ''
    this.data.wenid.map(item => {
      if (yy == '') {
        yy += item
      } else {
        yy += `,${item}`
      }
    })
    console.log(yy)
    app.request({
      url: '/whisky/add-gradevin?uid=' + wx.getStorageSync('uid') + '&wineids=' + yy,
      success: function (res) {
        console.log(res)
        if (res.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
          })
          setTimeout(wx.navigateTo({
            url: '/pages/index/index',
          }), 2000)

        } else {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
          })
        }
      }

    })
  },
  handlerAlphaTap(e) {
    let {
      ap
    } = e.target.dataset;
    this.setData({
      alpha: ap
    });
  },
  handlerMove(e) {
    let {
      list
    } = this.data;
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if (rY >= 0) {
      let index = Math.ceil((rY - this.apHeight) / this.apHeight);
      if (0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({
          alpha: nonwAp.alphabet
        });
      }
    }
  },
  filterData(e) {
    let name = e.currentTarget.dataset.country
    let index = e.currentTarget.dataset.index
    console.log(index)

    //切换后先还原数据
    this.data.list = this.data.countryTabData

    this.setData({
      countryIndex: index
    })
    if (name == '全部') {
      this.setData({
        list: this.data.list
      })
      return
    }

    var data = JSON.parse(JSON.stringify(this.data.list)); //深拷贝
    let datas = data.map(item => {
      let inData = item.datas.filter(itemb => {
        if (itemb.country == name) {
          return itemb
        }
      })
      item.datas = inData
      return item
    })
    console.log(datas)
    this.setData({
      list: datas
    })
  },
  onPageScroll(e) {
    if (e.scrollTop == 0) {
      this.setData({
        index: 0, //起始页
        des: 50
      })
      this.getData(this.data.typeId)
    }

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底')
    this.setData({
      index: this.data.index + 50,
      des: this.data.des + 50,
    })
    this.getData(this.data.typeId)
  },

})